package org.systm.adminuser.service.condition;

import lombok.Getter;
import org.springframework.data.jpa.domain.Specification;
import org.systm.adminuser.entity.UserRole;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import java.util.Collection;
import java.util.function.Function;

public class UserRoleQuery {
    @Getter
    private static Function<String, Specification<UserRole>> byRoleName = (roleName) -> (r, q, cb) -> {
        Expression<String> expression = r.get("role").as(String.class);
        return cb.equal(expression, roleName);
    };

    public static Function<Collection<String>, Specification<UserRole>> byRoleNames = (roleNames) -> (r, q, cb) -> {
        Path<Object> rolePath = r.get("role");
        Expression<Boolean> delEx = r.get("del").as(Boolean.class);
        return cb.and(rolePath.in(roleNames), cb.equal(delEx, Boolean.FALSE));
    };

}
